package pkg.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.ValidationError;
import pkg.dao.EntityDAO;
import pkg.entity.User;
import pkg.filter.UserFilter;

public class LoginActionBean extends BaseActionBean {

    private String targetUrl;
    private String loginId;
    private String password;
    private String hint;

//private Logger log = Logger.getLogger(LoginActionBean.class);
    public LoginActionBean() {
    }

    @DefaultHandler
    public Resolution view() {
        //log.debug("reseting user");
        resetUser();
        return new ForwardResolution("/login.jsp");
    }

    public Resolution login() {
        //log.debug("Entering login");
        if (loginId == null) {
            ValidationError error = new SimpleError("Login is required.");
            getContext().getValidationErrors().add("login", error);
            return getContext().getSourcePageResolution();
        }

        EntityDAO dao = new EntityDAO();
        UserFilter f = new UserFilter();
        f.setEmail(loginId);
        User user = (User) dao.findOneByFilter(f);

        if (user != null && user.getPassword().equals(password)) {
            getSession().setAttribute("user", user);
        } else {
            ValidationError error = new SimpleError("User name or password is wrong. Try again!");
            getContext().getValidationErrors().add("login", error);

            return getContext().getSourcePageResolution();
        }
        if (this.targetUrl != null) {
            //debug("Login redirecting to {0}", this.targetUrl);
            return new RedirectResolution(this.targetUrl);
        } else {
            return new RedirectResolution("/index.jsp");
        }
    }

    public Resolution findPassword() {
        resetUser();
        return new ForwardResolution("/ForgotPassword.jsp");
    }

    public Resolution forgotPassword() {
        //log.debug("Entering login");
        if (loginId == null) {
            ValidationError error = new SimpleError("Email is required.");
            getContext().getValidationErrors().add("login", error);
            return getContext().getSourcePageResolution();
        }

        EntityDAO dao = new EntityDAO();
        UserFilter f = new UserFilter();
        f.setEmail(loginId);
        User user = (User) dao.findOneByFilter(f);

        password = null;
        if (user != null && !user.getAdmin()) {
            password = user.getPassword();
        }
        return new ForwardResolution("/ShowPassword.jsp");
    }

    public Resolution logoff() {
        resetUser();
        return new RedirectResolution("/index.jsp");
    }

    public Resolution unauthorized() {
        return new RedirectResolution("/unauthorized.jsp");
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public String getLoginId() {
        return loginId;
    }

    public void setLoginId(String login) {
        this.loginId = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHint() {
        return hint;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }
}
